package com.xxkk.spring_boot_web.controller;

import com.alibaba.fastjson.JSONObject;
import com.xxkk.spring_boot_web.sdk.WAVE.ApiResponse;
import com.xxkk.spring_boot_web.sdk.WAVE.WAVESDK;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping(value = "/wave")
public class WAVECtl {

    private static final Logger logger = Logger.getLogger(WAVECtl.class);
    private final String fail = "FAIL";

    @RequestMapping("/guest_login")
    @CrossOrigin()
    @ResponseBody
    public String guest_login(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String phone = json.getString("phone");
            JSONObject header_map = iv_11_param(json);
            String res = WAVESDK.generateOtp(phone, header_map);
            logger.info(res);
            return res;
        } catch (Exception e) {
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }

    //验证码登录
    @RequestMapping("/code_login")
    @CrossOrigin()
    @ResponseBody
    public String code_login(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String phone = json.getString("phone");
            String otp = json.getString("otp");//验证码 012345
            JSONObject header_map = iv_11_param(json);

            String code_login_result = WAVESDK.confirmOtp(phone, otp, header_map);
            logger.info(code_login_result);
            //login
            String pin = json.getString("pin");
            if (pin == null)
                return code_login_result;
            String s1 = WAVESDK.securityToken(header_map);
            String s2 = WAVESDK.securityToken(header_map);

            String notificationtrackingid = json.getString("notificationtrackingid");
            if (notificationtrackingid == null)
                notificationtrackingid = "noti";
            ApiResponse login_result = WAVESDK.login(phone, pin, s1, s2, notificationtrackingid, header_map);
            logger.info("登录返回结果：\n" + login_result.toString());
            String receiverName = new org.json.JSONObject(login_result.getResponseBody()).getJSONObject("responseMap").getJSONObject("subscriberDetails").getString("name");
            String token = login_result.getHeaders();
            JSONObject return_login_result_json = new JSONObject();
            return_login_result_json.put("receiverName", receiverName);
            return_login_result_json.put("token", token);
            String return_login_result = return_login_result_json.toJSONString();
            logger.info(return_login_result);
            return return_login_result;
        } catch (Exception e) {
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }

    //查单
    @RequestMapping("/login")
    @CrossOrigin()
    @ResponseBody
    public String login(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String phone = json.getString("phone");
            String pin = json.getString("pin");
            JSONObject header_map = iv_11_param(json);

            String notificationtrackingid = json.getString("notificationtrackingid");
            if (notificationtrackingid == null)
                notificationtrackingid = "noti";

            String s1 = WAVESDK.securityToken(header_map);
            String s2 = WAVESDK.securityToken(header_map);
            ApiResponse login_result = WAVESDK.login(phone, pin, s1, s2, notificationtrackingid, header_map);
            logger.info("登录返回结果：\n" + login_result.getResponseBody());
            String receiverName = new org.json.JSONObject(login_result.getResponseBody()).getJSONObject("responseMap").getJSONObject("subscriberDetails").getString("name");
            String token = login_result.getHeaders();

            JSONObject return_login_result_json = new JSONObject();
            return_login_result_json.put("receiverName", receiverName);
            return_login_result_json.put("token", token);
            String return_login_result = return_login_result_json.toJSONString();
            logger.info(return_login_result);
            return return_login_result;
        } catch (Exception e) {//请求异常处理
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }

    //查询收款账号,核对收款方信息使用
    @RequestMapping("/find_receiver")
    @CrossOrigin()
    @ResponseBody
    public String find_receiver(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String token = json.getString("token");//wmt_mfs
            String receiverMsisdn = json.getString("receiverMsisdn");//账号
            String fingerprint = json.getString("fingerprint");
            if (fingerprint == null)
                fingerprint = "87EC104C0FFBB8E749CD59D9C64851441B38D1C13C9746DC124BB9E71E66DCB9";
            JSONObject header_map = iv_8_param(json);

            ApiResponse res_ = WAVESDK.checkBeneficiary(token, receiverMsisdn, fingerprint, header_map);
            String res = JSONObject.toJSONString(res_);
            logger.info(res);
            return res;
        } catch (Exception e) {//请求异常处理
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }

    /**
     * 转账
     * receiverName收款者名字, receiverMsisdns收款者账号
     * senderName转账人名字,
     * amount转账金额 , note转账备注
     */
    @RequestMapping("/transfer_money")
    @CrossOrigin()
    @ResponseBody
    public String transfer_money(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String token = json.getString("token");
            String receiverName = json.getString("receiverName");
            String receiverMsisdn = json.getString("receiverMsisdn");
            String senderName = json.getString("senderName");
            String amount = json.getString("amount");
            String pin = json.getString("pin");
            String note = json.getString("note");
//            String s1 = json.getString("s1");
            String deviceid = json.getString("deviceId");
            if (deviceid == null)
                deviceid = "2a8ace2353237e1ac4c95f268f6f757f299eaa83";
            JSONObject header_map = iv_11_param(json);

            String s1 = WAVESDK.securityToken(header_map);
//            String s1 = new org.json.JSONObject(securityToken).getJSONObject("responseMap").getString("securityCounter");
            System.out.println("s1 => " + s1);

            ApiResponse res = WAVESDK.sendMoney(deviceid, token, receiverName, receiverMsisdn, senderName, amount, pin, note, s1);
            JSONObject ret_obj = new JSONObject();
            ret_obj.put("new_token", res.getHeaders());
            ret_obj.put("responseBody", res.getResponseBody());
            ret_obj.put("statusCode", res.getStatusCode());
            String res_str = ret_obj.toJSONString();
            logger.info(res_str);
            return res_str;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fail;
    }


    @RequestMapping("/histories")
    @CrossOrigin()
    @ResponseBody
    public String histories(@RequestBody Map<String, Object> cipherMap, HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject json = new JSONObject(cipherMap);
            String token = json.getString("token");
            JSONObject header_map = iv_8_param(json);
            String url = json.getString("url");
            ApiResponse res_ = WAVESDK.histories(token, header_map, url);
            return res_.getResponseBody();
        } catch (Exception e) {
            logger.info("http请求异常：" + e.getMessage());
        }
        return fail;
    }


    private JSONObject iv_11_param(JSONObject json) {
        String deviceid = json.getString("deviceId");
        String device = json.getString("device");
        String cpuabi = json.getString("cpuabi");
        String manufacturer = json.getString("manufacturer");
        String model = json.getString("model");
        String product = json.getString("product");
        String osversion = json.getString("osversion");
        String appversion = json.getString("appversion");
        String versioncode = json.getString("versioncode");
        String userlanguage = json.getString("userlanguage");
        String appid = json.getString("appid");

        if (device == null)
            device = "OnePlus 7T(HD1900)";
        if (cpuabi == null)
            cpuabi = "arm64-v8a,armeabi-v7a,armeabi";
        if (manufacturer == null)
            manufacturer = "OnePlus";
        if (model == null)
            model = "HD1900";
        if (product == null)
            product = "OnePlus7T";
        if (osversion == null)
            osversion = "11";
        if (appid == null)
            appid = "mm.com.wavemoney.wavepay";
        if (appversion == null)
            appversion = "2.3.1";
        if (versioncode == null)
            versioncode = "1463";
        if (userlanguage == null)
            userlanguage = "en";

        if (deviceid == null)
            deviceid = "2a8ace2353237e1ac4c95f268f6f757f299eaa83";

        JSONObject header_map = new JSONObject();
        header_map.put("deviceid", deviceid);
        header_map.put("device", device);
        header_map.put("cpuabi", cpuabi);
        header_map.put("manufacturer", manufacturer);
        header_map.put("model", model);
        header_map.put("product", product);
        header_map.put("osversion", osversion);
        header_map.put("appid", appid);
        header_map.put("appversion", appversion);
        header_map.put("versioncode", versioncode);
        header_map.put("userlanguage", userlanguage);
        return header_map;
    }

    private JSONObject iv_8_param(JSONObject json) {
        String deviceid = json.getString("deviceId");
        String device = json.getString("device");
        String product = json.getString("product");
        String osversion = json.getString("osversion");
        String appversion = json.getString("appversion");
        String versioncode = json.getString("versioncode");
        String userlanguage = json.getString("userlanguage");
        String appid = json.getString("appid");

        if (device == null)
            device = "OnePlus 7T(HD1900)";
        if (product == null)
            product = "OnePlus7T";
        if (osversion == null)
            osversion = "11";
        if (appid == null)
            appid = "mm.com.wavemoney.wavepay";
        if (appversion == null)
            appversion = "2.3.1";
        if (versioncode == null)
            versioncode = "1463";
        if (userlanguage == null)
            userlanguage = "en";

        if (deviceid == null)
            deviceid = "2a8ace2353237e1ac4c95f268f6f757f299eaa83";

        JSONObject header_map = new JSONObject();
        header_map.put("deviceid", deviceid);
        header_map.put("device", device);
        header_map.put("product", product);
        header_map.put("osversion", osversion);
        header_map.put("appid", appid);
        header_map.put("appversion", appversion);
        header_map.put("versioncode", versioncode);
        header_map.put("userlanguage", userlanguage);
        return header_map;
    }

}
